import { Component, OnInit } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import {ApiService} from "../../services/api.service";
import {CursorService} from "@core/http/cursor.service";

@Component({
  selector: 'app-combo-detail',
  templateUrl: './combo-detail.component.html',
  styleUrls: ['./combo-detail.component.scss'],
  providers:[ApiService]
})
export class ComboDetailComponent implements OnInit {
  public id
  public activityList:any = [];
  public groupList:any = [];
  public productList:any = [];
  public groupData: any;
  public catalogList =[];
  constructor(private route: ActivatedRoute,
              public cursorService: CursorService,
              private router: Router,
              private apiService: ApiService) {
  }

  ngOnInit() {
    this.id = this.route.snapshot.paramMap.get('id')
    this.apiService.getcomboDetail(this.id).subscribe(data => {
        this.groupData = data
      })
    this.cursorService.limit =100;
    this.cursorService.statement= "select * where groupcode="+"'"+this.id+"'"
    this.apiService.getproductNum(this.cursorService).subscribe(data => {
      for(let item of data){
        this.catalogList.push({catalogname:item['catalogname'],catalogno:item['catalogno'],content:[]})
      }
       //目录去重
      this.catalogList =this.unique(this.catalogList);
      for(let item of data){
        for(let item2 of this.catalogList){
          let content =[];
          if(item['catalogno']==item2['catalogno']){
            item2['content'].push(item)
          }
        }
      }
    })

    // this.getComboDetail()

    // this.apiServicemark.getActivityList().subscribe(data => {
    //   this.activityList = data
    // })
    //
    // this.apiServicecustomer.getCustomerGroupList().subscribe(data => {
    //   this.groupList = data
    // })
  }
//目录列表去重
  unique(arr) {
    let result = [];
    let obj = {};
    for (let i = 0; i < arr.length; i++) {
      if (!obj[arr[i]['catalogno']]) {
        result.push(arr[i]);
        obj[arr[i]['catalogno']] = true;
      }
    }
    return result;
  }
  getComboDetail() {}

  public jumpTemplate(){
     this.router.navigate(['/marketing/template-detail'], { replaceUrl: true })
  }
}
